<?php

namespace app\common;

use GuzzleHttp\Psr7\Stream;
use Webman\Http\Response;

class StreamResponse extends Response
{
    protected $stream;

    public function __construct(int $status = 200, array $headers = [], callable $callback = null)
    {
        $this->stream = new Stream(fopen('php://temp', 'wb+'));
        call_user_func($callback, $this->stream);
        parent::__construct($status, $headers, $this->stream);
    }

    public static function create(callable $callback, int $status = 200, array $headers = [])
    {
        return new self($status, $headers, $callback);
    }
}
